package com.freewater.library.lending.book.infrastructure.dao;

import com.freewater.library.lending.book.infrastructure.po.BookInstancePo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface BookInstanceDao extends JpaRepository<BookInstancePo, Long> {

    Optional<BookInstancePo> findByQrCode(String qrCode);

    boolean existsByQrCode(String qrCode);

    @Query("from BookInstancePo t where t.isbn = :isbn " +
            "and t.takenOff = false and t.reserved = false " +
            "and t.checkedOut = false and t.lost = false ")
    List<BookInstancePo> findCanBeReservedBooks(@Param("isbn") String isbn);

    @Query("from BookInstancePo t where " +
            "t.takenOff = false and t.checkedOut = false " +
            "and t.lost = false and t.reserved = true ")
    List<BookInstancePo> findReservingBooks();

    @Query("from BookInstancePo t where t.checkedOut = true and t.reserved = false " +
            "and t.checkedOutOverdueMarked = false ")
    List<BookInstancePo> findCheckingOutBooksNotMarkedOverdue();
}
